package com.easy.frame.servers.sys.mapping;

import com.easy.frame.base.basic.constant.DbConstants;
import com.easy.frame.base.basic.pojo.MenuContent;
import com.easy.frame.base.basic.pojo.vo.MenuContentVO;
import com.easy.frame.base.basic.pojo.vo.SysStaffAllVO;
import com.easy.frame.base.basic.util.AuthUtils;
import com.easy.frame.servers.sys.entity.SysMenu;
import com.easy.frame.servers.sys.vo.SysMenuListVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统字典表实体对象映射方法类
 *
 * @author zzf
 * @date 2020/7/17 15:11
 */
@Mapper
public interface SysMenuMapping {

    SysMenuMapping INSTANCE = Mappers.getMapper(SysMenuMapping.class);

    /**
     * 持久化对象转数据传输对象
     *
     * @param dict
     * @return
     */
    @Mapping(source = "enName", target = "name")
    default SysMenuListVO po2ListVO(SysMenu dict) {
        SysMenuListVO menuListVO = new SysMenuListVO();
        menuListVO.setLevel(dict.getLevel());
        menuListVO.setPath(dict.getPath());
        menuListVO.setParentPath(dict.getParentPath());
        menuListVO.setId(dict.getId());
        menuListVO.setOrderNum(dict.getOrderNum());
        menuListVO.setLeafNode(dict.getLeafNode());
        if (AuthUtils.getCurrentUserLanguage().equals(DbConstants.LANGUAGE_CN)) {
            menuListVO.setName(dict.getName());
        } else if (AuthUtils.getCurrentUserLanguage().equals(DbConstants.LANGUAGE_FT)) {
            menuListVO.setName(dict.getFtName());
        } else {
            menuListVO.setName(dict.getEnName());
        }
        return menuListVO;
    }

    /**
     * 持久化对象转数据传输对象
     *
     * @param entity   菜单集合
     * @param language 当前用户语言
     */
    default List<SysStaffAllVO.Menu> listPo2vo(List<SysMenu> entity, Integer language) {
        List<SysStaffAllVO.Menu> result = new ArrayList<>();
        entity.forEach(sysMenu -> {
            SysStaffAllVO.Menu menu = new SysStaffAllVO.Menu();
            menu.setLevel(sysMenu.getLevel());
            menu.setPath(sysMenu.getPath());
            menu.setParentPath(sysMenu.getParentPath());
            menu.setId(sysMenu.getId());
            menu.setOrderNum(sysMenu.getOrderNum());
            menu.setLeafNode(sysMenu.getLeafNode());
            if (language.equals(DbConstants.LANGUAGE_CN)) {
                menu.setName(sysMenu.getName());
            } else if (language.equals(DbConstants.LANGUAGE_FT)) {
                menu.setName(sysMenu.getFtName());
            } else {
                menu.setName(sysMenu.getEnName());
            }
            result.add(menu);
        });
        return result;
    }

    /**
     * 持久化对象转数据传输对象
     *
     * @param content
     * @return
     */
    default MenuContentVO contentPo2ListVO(MenuContent content) {
        MenuContentVO contentVO = new MenuContentVO();
        contentVO.setKey(content.getKey());
        contentVO.setOrderNum(content.getOrderNum());
        if (AuthUtils.getCurrentUserLanguage().equals(DbConstants.LANGUAGE_CN)) {
            contentVO.setValue(content.getValue());
        } else if (AuthUtils.getCurrentUserLanguage().equals(DbConstants.LANGUAGE_FT)) {
            contentVO.setValue(content.getFtValue());
        } else {
            contentVO.setValue(content.getEnValue());
        }
        contentVO.setHad(false);
        return contentVO;
    }

}



